package com.aizhixin.lab.course.course.service;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.core.PageData;
import com.aizhixin.lab.common.core.PageUtil;
import com.aizhixin.lab.common.domain.PageDomain;
import com.aizhixin.lab.course.course.dto.CourseAssistDirDTO;
import com.aizhixin.lab.course.course.dto.CourseAssistFileDTO;
import com.aizhixin.lab.course.course.entity.CourseAssist;
import com.aizhixin.lab.course.course.repository.CourseAssistRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class TeacherCourseAssistService {
    @Autowired
    private CourseAssistRepository assistRepository;

    public PageData<CourseAssist> getList(String courseId, Long parentId, Integer pageNumber, Integer pageSize) {
        Pageable pageable = PageUtil.createRequestAndSortType(pageNumber, pageSize, "desc", "id");
        Page<CourseAssist> page;
        if (parentId != null && parentId.longValue() > 0) {
            page = assistRepository.findByCourseIdAndParentIdAndDeleteFlag(pageable, courseId, parentId, DataValidity.VALID.getState());
        } else {
            page = assistRepository.findByCourseIdAndDeleteFlagAndParentIdIsNull(pageable, courseId, DataValidity.VALID.getState());
        }
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageSize(page.getSize());
        pageDomain.setPageNumber(page.getNumber());
        pageDomain.setTotalElements(page.getTotalElements());
        pageDomain.setTotalPages(page.getTotalPages());

        PageData<CourseAssist> pageData = new PageData<>();
        pageData.setData(page.getContent());
        pageData.setPage(pageDomain);
        return pageData;
    }

    public PageData<CourseAssist> getListWithoutHide(String courseId, Long parentId, Integer pageNumber, Integer pageSize) {
        Pageable pageable = PageUtil.createRequestAndSortType(pageNumber, pageSize, "desc", "id");
        Page<CourseAssist> page;
        if (parentId != null && parentId.longValue() > 0) {
            page = assistRepository.findByCourseIdAndParentIdAndDeleteFlagAndIsHide(pageable, courseId, parentId, DataValidity.VALID.getState(), false);
        } else {
            page = assistRepository.findByCourseIdAndDeleteFlagAndIsHideAndParentIdIsNull(pageable, courseId, DataValidity.VALID.getState(), false);
        }
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageSize(page.getSize());
        pageDomain.setPageNumber(page.getNumber());
        pageDomain.setTotalElements(page.getTotalElements());
        pageDomain.setTotalPages(page.getTotalPages());

        PageData<CourseAssist> pageData = new PageData<>();
        pageData.setData(page.getContent());
        pageData.setPage(pageDomain);
        return pageData;
    }

    public Map<String, Object> saveDir(CourseAssistDirDTO dto) {
        Map<String, Object> result = new HashMap<>();
        CourseAssist assist = null;
        if (dto.getId() != null && dto.getId().longValue() > 0) {
            assist = assistRepository.findByIdAndDeleteFlag(dto.getId(), DataValidity.VALID.getState());
        }
        if (assist == null) {
            assist = new CourseAssist();
            assist.setIsDir(true);
            assist.setIsHide(false);
        }
        BeanUtils.copyProperties(dto, assist);
        if (assist.getParentId() != null && assist.getParentId().longValue() == 0) {
            assist.setParentId(null);
        }
        assistRepository.save(assist);
        result.put(ApiReturnConstants.SUCCESS, true);
        return result;
    }

    public Map<String, Object> updateDir(CourseAssistDirDTO dto) {
        Map<String, Object> result = new HashMap<>();
        if (dto.getId() != null){
            CourseAssist assist = new CourseAssist();
            BeanUtils.copyProperties(dto, assist);
            assist.setIsDir(true);
            assistRepository.save(assist);
            result.put(ApiReturnConstants.SUCCESS, true);
            return result;
        }
        result.put(ApiReturnConstants.SUCCESS, false);
        return result;
    }

    public Map<String, Object> saveFile(CourseAssistFileDTO dto) {
        Map<String, Object> result = new HashMap<>();
        CourseAssist assist = new CourseAssist();
        assist.setIsDir(false);
        assist.setIsHide(false);
        BeanUtils.copyProperties(dto, assist);
        if (assist.getParentId() != null && assist.getParentId().longValue() == 0) {
            assist.setParentId(null);
        }
        assistRepository.save(assist);
        result.put(ApiReturnConstants.SUCCESS, true);
        return result;
    }

    public Map<String, Object> hide(Long assistId) {
        Map<String, Object> result = new HashMap<>();
        CourseAssist assist = assistRepository.findOne(assistId);
        if (assist != null) {
            boolean flag = false;
            if (assist.getIsHide() != null) {
                flag = assist.getIsHide();
            }
            assist.setIsHide(!flag);
            assistRepository.save(assist);
            result.put(ApiReturnConstants.SUCCESS, true);
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
        }
        return result;
    }

    public Map<String, Object> delete(Long assistId) {
        Map<String, Object> result = new HashMap<>();
        CourseAssist assist = assistRepository.findOne(assistId);
        if (assist != null) {
            if (assist.getIsDir()) {
                deleteFile(assist.getId());
            }
            assist.setDeleteFlag(DataValidity.INVALID.getState());
            assistRepository.save(assist);
            result.put(ApiReturnConstants.SUCCESS, true);
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
        }
        return result;
    }

    public void deleteFile(Long parentId) {
        List<CourseAssist> list = assistRepository.findByParentId(parentId);
        if (list != null && !list.isEmpty()) {
            for (CourseAssist item : list) {
                if (item.getIsDir()) {
                    deleteFile(item.getId());
                }
                item.setDeleteFlag(DataValidity.INVALID.getState());
                assistRepository.save(item);
            }
        }
    }
}
